﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.Dialog.Role
{
    public partial class AuthRoleDia : UIEditForm
    {
        public BaseRoleModel roleModel;
        public RoleInfoControler _my_ctrl;

        public AuthRoleDia()
        {
            InitializeComponent();
        }

        public AuthRoleDia(int roid, RoleInfoControler _my_ctrl)
        {
            this._my_ctrl = _my_ctrl;
            this.roleModel = _my_ctrl.getRoleInfo(roid);
            InitializeComponent();
        }



        private static void SetCheckedChildNodes(TreeNode tn, bool CheckState)
        {
            if (tn.Nodes.Count > 0)
            {
                //当前树节点状态变更，子树节点同步状态
                foreach (TreeNode tn1 in tn.Nodes)
                {
                    tn1.Checked = CheckState;

                    //SetCheckedChildNodes(tn1, CheckState);
                }
            }
        }

        protected override bool CheckData()
        {
            //return CheckEmpty("", "请输入角色名称");
            return false;
        }


        private void InitTreeView()
        {
            var menus = _my_ctrl.getMenuTreeNodes(roleModel.id);
            TreeNode TopNode = new TreeNode("角色授权", menus.ToArray());
            this.uiTreeView1.Nodes.Add(TopNode);
            this.uiTreeView1.TopNode.Toggle();

        }

        private void AuthRoleDia_Load(object sender, EventArgs e)
        {
            Text = "角色授权:" + roleModel.role_name;
            InitTreeView();
        }


        private List<BaseRoleMenuModel> getTreeCheckNode(TreeNode tn, List<BaseRoleMenuModel> roleMenu = null)
        {
            if (roleMenu == null)
            {
                roleMenu = new List<BaseRoleMenuModel>();
            }
            foreach (TreeNode tn1 in tn.Nodes)
            {
                //循环遍历次节点是否存在勾选，只要有一个次节点勾选，主节点都默认勾选，避免查不到权限
                foreach (TreeNode childNode in tn1.Nodes)
                {
                    if (childNode.Checked)
                    {
                        tn1.Checked = true;
                        break;
                    }
                }
                if (tn1.Checked || tn1.IsExpanded)
                {
                    roleMenu.Add(new BaseRoleMenuModel
                    {
                        base_role_id = roleModel.id,
                        base_menu_id = tn1.Name.ToString().ToInt(),
                        create_date = DateTime.Now,
                        create_user = MainFrm.LoginUser.user_name
                    });
                    getTreeCheckNode(tn1, roleMenu);
                }
            }
            return roleMenu;

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 查找最大子节点，避免因为单击焦点问题没能获取全部权限
            TreeNode targetNode = null;
            foreach (TreeNode node in this.uiTreeView1.Nodes)
            {
                if (node.Text == "角色授权")
                {
                    targetNode = node;
                    break;
                }
            }
            if (targetNode != null)
            {
                uiTreeView1.SelectedNode = targetNode;
            }
            #endregion
            var authRoles1 = getTreeCheckNode(this.uiTreeView1.TopNode);
            _my_ctrl.updateAuthRole(this.roleModel.id, authRoles1);
            this.Close();
        }

        private TreeNode FindDeepestNode(TreeNode parentNode)
        {
            TreeNode result = parentNode;

            foreach (TreeNode node in parentNode.Nodes)
            {
                TreeNode deepestNode = FindDeepestNode(node);

                if (deepestNode.FullPath.Split('\\').Length > result.FullPath.Split('\\').Length)
                {
                    result = deepestNode;
                }
            }

            return result;
        }
    }
}
